package com.ym.controller.patient.self;

import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.PatientDetailEntity;
import com.ym.medical.domain.entity.PatientHealthRecordsEntity;
import com.ym.medical.domain.param.AddQuestionerParam;
import com.ym.medical.service.IPatientCenterSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 我的家人
 *
 * @author: 李涛
 * @version: 2019年05月14日 19:36
 */
@Api("我的家人")
@Controller
@ApiSign
@RequestMapping(value = "patient/self/family", headers = Constants.Api.VERSION)
public class MyFamilyController extends BaseController {
	private static Logger logger = LoggerFactory.getLogger(MyFamilyController.class);

	@Autowired
	private IPatientCenterSV iPatientCenterSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("家人健康档案")
	@GetMapping(value = "/selectArchives")
	@ResponseBody
	public APIResponse<PatientHealthRecordsEntity> selectArchives(
			@ApiParam(name = "patientId", value = "患者id", required = true)
			@RequestParam Long patientId) throws Exception {
		PatientHealthRecordsEntity healthRecords = iPatientCenterSV.selectArchives(patientId);
		return successData(healthRecords);

	}

	@Log
	@ApiOperation("家人健康档案添加")
	@PostMapping(value = "/addArchives", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse addArchives(
			@ApiParam(name = "patientHealthRecords", value = "健康档案对象", required = true)
			 @RequestBody PatientHealthRecordsEntity patientHealthRecords) throws Exception {
		PatientHealthRecordsEntity records = iPatientCenterSV.addArchives(patientHealthRecords);
		if (null == records) {
			return failMsg("操作失败");
		} else {
			return successData(records);
		}

	}


	@Log
	@ApiOperation("家人健康档案更改")
	@PostMapping(value = "/upArchives", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody

	public APIResponse upArchives(
			@ApiParam(name = "patientHealthRecords", value = "健康档案对象", required = true)
			 @RequestBody PatientHealthRecordsEntity patientHealthRecords) throws Exception {
		int rows = iPatientCenterSV.upArchives(patientHealthRecords);

		if (rows != 1) {
			return failMsg("操作失败");
		}
		return successMsg("操作成功");


	}

	@Log
	@ApiOperation("删除家人")
	@GetMapping(value = "/deletePatient")
	@ResponseBody
	public APIResponse deletePerson(
			@ApiParam(name = "patientId", value = "家人id", required = true)
					Long patientId) throws Exception {
		int rows = iPatientCenterSV.deletePerson(patientId);
		if (rows != 1) {
			return failMsg("操作失败");
		}
		return successMsg("操作成功");

	}


	@Log
	@ApiOperation("好友添加")
	@PostMapping(value = "/addOthers", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse addOthers(
			@ApiParam(name = "patientDetail", value = "患者对象", required = true)
			@RequestBody AddQuestionerParam patientDetail) throws Exception {
		Long userId = ShiroUtil.getLoginId();
		Long id = iPatientCenterSV.addOthers(patientDetail, userId);
		if (id < 0) {
			return failMsg("操作失败");
		}
		return successData(id);

	}

	@Log
	@ApiOperation("家人信息修改")
	@PostMapping(value = "/updatePatient", consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public APIResponse updatePatient(
			@ApiParam(name = "patientDetail", value = "患者对象", required = true)
			@RequestBody PatientDetailEntity patientDetail) throws Exception {
		int rows = iPatientCenterSV.updatePatient(patientDetail);
		if (rows != 1) {
			return failMsg("操作失败");
		}
		return successMsg("操作成功");

	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("患者检索")
	@GetMapping(value = "/selectAllPatient")
	@ResponseBody
	public APIResponse selectAllPatient() {


		return success();

	}


}
